package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.UserUnder;
import com.ccp.dev.system.service.UserUnderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 下属管理 控制器类
 * @author y
 */
@Controller
@Action(ownermodel = SysAuditModelType.USER_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/userUnder")
public class UserUnderController extends SysBaseController {
	@Resource
	private UserUnderService userUnderService;

	/**
	 * 添加用户上下级
	 * @param userUnder model
	 * @return ResultData
	 */
	@Action(description = "添加用户上下级",
			detail = "添加用户上下级<#if success>成功<#else>失败</#if>",
			exectype = "编辑日志")
	@ResponseBody
	@RequestMapping("/add")
	public ResultData add(UserUnder userUnder) {
		try{
			this.userUnderService.add(userUnder);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("save.success");
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("save.failure");
		}
	}

	/**
	 * 批量添加用户下级
	 * @param request HttpServletRequest
	 * @param userId 用户id
	 * @return ResultData
	 */
	@Action(description = "批量添加用户下级",
			detail = "<#assign entity=sysUserService.getOneById(userId)/>给用户【${entity.fullName}】批量添加下级<#if success>成功<#else>失败</#if>",
			exectype = "编辑日志")
	@ResponseBody
	@RequestMapping("/batchAddUserUnder")
	public ResultData batchAddUserUnder(HttpServletRequest request,@RequestParam String userId) {
		try{
			String[] userUnders = RequestUtil.getStringAryByStr(request,"userUnderId");
			this.userUnderService.addUserUnder(userId,userUnders);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("save.success");
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("save.failure");
		}
	}

	/**
	 * 取得下属管理分页列表
	 * @param request HttpServletRequest
	 * @return ResultData
	 */
	@Action(description = "取得下属管理分页列表",
			detail = "取得下属管理分页列表<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
	@RequestMapping("/list")
	public ResultData list(HttpServletRequest request) {
		try{
			QueryFilter queryFilter=new QueryFilter(request);
			List<UserUnder> list=this.userUnderService.queryList(queryFilter);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),list);
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}
	
	/**
	 * 删除下属管理
	 * @param request HttpServletRequest
	 */
	@ResponseBody
	@RequestMapping("/del")
	@Action(description = "删除下属管理",
			detail = "根据主键删除下属管理<#if success>成功<#else>失败</#if>",
			exectype = "删除日志")
	public ResultData del(HttpServletRequest request) {
		try{
			String[] lAryId = RequestUtil.getStringAryByStr(request, "id");
			this.userUnderService.delByIdArray(lAryId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("delete.success");
		} catch(Exception ex){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("delete.failure");
		}
	}

	/**
	 * 根据用户id查询下属
	 * @param request HttpServletRequest
	 * @return ResultData
	 */
	@Action(description = "根据用户id查询下属",
			detail = "<#assign entity=sysUserService.getOneById(userId)/>查询【${entity.fullName}】下属信息<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
    @RequestMapping("/queryUnderByUserId")
	public ResultData queryUnderByUserId(HttpServletRequest request){
		try {
			//needPage 根据前台使用方法不同特意传一个参数是否要分页  0 不分页 1 分页
			long needPage = RequestUtil.getLong(request,"needPage",1);
			QueryFilter queryFilter;
			if(needPage == 0){
				queryFilter = new QueryFilter(request,false);
			    List<UserUnder> userUnderList = this.userUnderService.queryUnderByUserId(queryFilter);
				SysAuditThreadLocalHolder.putParamerter("success", true);
                return getResultDataSuccess("operation.success",0L,userUnderList);
			}
            queryFilter = new QueryFilter(request);
            List<UserUnder> userUnderList = this.userUnderService.queryUnderByUserId(queryFilter);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),userUnderList);
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}
	/**
	 * 根据用户id查询上级
	 * @param userId 用户id
	 * @return ResultData
	 */
	@Action(description = "根据用户id查询上级",
			detail = "查询<#if StringUtil.isNotEmpty(userId)><#assign entity=sysUserService.getOneById(userId)/>【${entity.fullName}】</#if>上级信息<#if success>成功<#else>失败</#if>",
			exectype = "查询日志")
	@ResponseBody
	@RequestMapping("/querySuperiorByUserId")
	public ResultData querySuperiorByUserId(@RequestParam String userId){
		try {
			List<UserUnder> userUnderList = this.userUnderService.querySuperiorByUserId(userId);
			SysAuditThreadLocalHolder.putParamerter("success", true);
			return getResultDataSuccess("operation.success",0,userUnderList);
		}catch (Exception e){
			SysAuditThreadLocalHolder.putParamerter("success", false);
			return getResultDataError("operation.failure");
		}
	}
}
